package pack0902;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TongXinJIeShou {
    public static void main(String[] args) throws Exception{
        Socket socket = new Socket("192.168.1.161",9989);
        System.out.println("已经连接客户端");
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();

        Txjs1 txjs1 = new Txjs1(os);
        Txjs2 txjs2 = new Txjs2(is);
        txjs1.start();
        txjs2.start();
    }
}
class Txjs1 extends Thread{
    OutputStream os;

    public Txjs1(OutputStream os) {
        this.os = os;
    }

    public void run() {
        Scanner in = new Scanner(System.in);
        try {
            while (true){
                String str = in.next();
                if ("exit".equals(str)){
                    break;
                }
                os.write(str.getBytes());
                Thread.sleep(1000);
            }
            os.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
class Txjs2 extends Thread{
    InputStream is;

    public Txjs2(InputStream is) {
        this.is = is;
    }

    public void run() {
        try {
            byte[] bytes = new byte[100];
            while (true){
                int len = is.read(bytes);
                String str = new String(bytes,0,len);
                System.out.println(str);
                Thread.sleep(1000);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

